/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.a11y.touch;

import android.app.Activity;
import ch.bfh.aeneas.core.a11y.touch.strategy.InputHandlingStrategy;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import ch.bfh.aeneas.core.a11y.touch.strategy.DefaultStrategy;

/**
 *
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class TouchLayer extends View implements View.OnClickListener, View.OnTouchListener, ViewGroup.OnHierarchyChangeListener {
    
    protected InputHandlingStrategy mStrategy;
    protected ViewGroup mContainer;
    protected Context mContext;
    protected Boolean mHandled = false;
    private final static String LOG_TAG = "TouchLayer";
    
    public TouchLayer(Context context) {
        this(context, new DefaultStrategy());
        mContext = context;
    }
    
    public TouchLayer(Context context, InputHandlingStrategy strategy)
    {
        super(context);
        mStrategy = strategy;
        mContext = context;
    }
    
    public TouchLayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }
    
    public TouchLayer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onAttachedToWindow() {
        
        /*if(!mHandled && mContext instanceof Activity)
        {
            Activity app = (Activity) super.getContext();
            mContainer = new FrameLayout(app);
            mContainer.setOnHierarchyChangeListener(this);
            
            View root = null;
            //View root = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
            if(root == null)
                Log.e(LOG_TAG, "root is null?!");
            else
                mContainer.addView(root);
            
            ViewGroup parent = getParentView(this);
            if(parent != null) {
                parent.removeView(this);
                mContainer.addView(this);
            }
            else
            {
                Log.e(LOG_TAG,"parent is null too!");
            }
            
            mHandled = true;
            app.setContentView(mContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            
            super.setOnClickListener(this);
            super.setOnTouchListener(this);
        }//*/
        
        if(!mHandled) {
            ViewGroup parent = getParentView(this);
            if(parent != null) {
                parent.removeView(this);
            }
            
            mHandled = true;
            
            Activity app = (Activity) mContext;
            app.addContentView(this, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            
            super.setOnClickListener(this);
            super.setOnTouchListener(this);
        }//*/
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);//*/
        /*Paint p = new Paint();
        p.setARGB(255,0,0,255);
        canvas.drawRect(10,10,10,10, p);//*/
    }
    
    protected ViewGroup getParentView(View arg0) {
        ViewParent parent = arg0.getParent();
        Boolean isGroup = (parent instanceof ViewGroup);
        if(!isGroup && parent != null)
            return getParentView((View)parent);
        else if(isGroup)
            return (ViewGroup)parent;
        else
            return null;
    }

    public void onChildViewAdded(View arg0, View arg1) {
        mContainer.bringChildToFront(this);
    }

    public void onChildViewRemoved(View arg0, View arg1) {
        mContainer.bringChildToFront(this);
    }

    public boolean onTouch(View arg0, MotionEvent arg1) {
        
        if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
            String msg = String.format("Touch down event on %d (System) and %d (Event)", System.currentTimeMillis(),arg1.getEventTime());
            Log.e(LOG_TAG,msg);
        } else if(arg1.getAction() == MotionEvent.ACTION_UP) {
            String msg = String.format("Touch up event on %d (System) and %d (Event)", System.currentTimeMillis(),arg1.getEventTime());
            Log.e(LOG_TAG,msg);
        }
        //arg0.dispatchTouchEvent(arg1); <-- dispatch event
        
        return false;
    }

    public void onClick(View arg0) {
        String msg = String.format("Click event on %d", System.currentTimeMillis());
        Log.e(LOG_TAG,msg);
    }
}
